home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / comm / msged400.zip / src / dirute.c < prev    next >
C/C++ Source or Header  |  1996-07-24  |  8KB  |  415 lines

  1. /*
  2.  *  DIRUTE.C
  3.  *
  4.  *  Written on 30-Jul-90 by jim nutt and released to the public domain.
  5.  */
  6.  
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <errno.h>
  10.  
  11. #include "dirute.h"
  12.  
  13. #if defined(OS216)
  14. #include <dos.h>
  15. #include <sys\types.h>
  16. #define INCL_DOSPROCESS
  17. #include <os2.h>
  18. #include <sys\stat.h>
  19. #include <ctype.h>
  20. #include <string.h>
  21. #include <io.h>
  22. #include <fcntl.h>
  23. #include <time.h>
  24.  
  25. union
  26. {
  27.     FDATE a;
  28.     unsigned int b;
  29. }
  30. dc;
  31.  
  32. union
  33. {
  34.     FTIME a;
  35.     unsigned int b;
  36. }
  37. tc;
  38.  
  39. static struct _FILEFINDBUF InfoBuf;
  40.  
  41. HDIR hDir;
  42. USHORT cSearch;
  43. USHORT usAttrib;
  44.  
  45. #define FILENAMELEN 255
  46.  
  47. int dir_findfirst(char *filename, int attribute, struct _dta *dta)
  48. {
  49.     hDir = 0x0001;
  50.     usAttrib = attribute;
  51.     cSearch = 1;
  52.  
  53.     if (DosFindFirst2(filename
  54.                       ,&hDir
  55.                       ,usAttrib
  56.                       ,&InfoBuf
  57.                       ,(USHORT) (sizeof(InfoBuf) * cSearch)
  58.                       ,&cSearch
  59.                       ,FIL_STANDARD
  60.                       ,(ULONG) NULL) != 0)
  61.     {
  62.         DosFindClose(hDir);
  63.         errno = ENOENT;
  64.         return (-1);
  65.     }
  66.     else
  67.     {
  68.         dta->attrib = (char)InfoBuf.attrFile;
  69.         dta->size = InfoBuf.cbFile;
  70.         dc.a = InfoBuf.fdateLastWrite;
  71.         tc.a = InfoBuf.ftimeLastWrite;
  72.         dta->wr_time = tc.b;
  73.         dta->wr_date = dc.b;
  74.         strcpy(dta->name, InfoBuf.achName);
  75.         errno = 0;
  76.         return (0);
  77.     }
  78. }
  79.  
  80. int dir_findnext(struct _dta *dta)
  81. {
  82.  
  83.     if ((DosFindNext(hDir
  84.                      ,&InfoBuf
  85.                      ,(USHORT) (FILENAMELEN + 23)
  86.                      ,&cSearch)
  87.         ) || (cSearch != 1))
  88.     {
  89.         DosFindClose(hDir);
  90.         errno = ENOENT;
  91.         return (-1);
  92.     }
  93.     else
  94.     {
  95.         dta->attrib = (char)InfoBuf.attrFile;
  96.         dta->size = InfoBuf.cbFile;
  97.         dc.a = InfoBuf.fdateLastWrite;
  98.         tc.a = InfoBuf.ftimeLastWrite;
  99.         dta->wr_time = tc.b;
  100.         dta->wr_date = dc.b;
  101.         strcpy(dta->name, InfoBuf.achName);
  102.         errno = 0;
  103.         return (0);
  104.     }
  105. }
  106.  
  107. #elif defined(OS2)
  108. #define INCL_DOSPROCESS
  109. #include <os2.h>
  110.  
  111. #include <string.h>
  112. #include <errno.h>
  113.  
  114. union
  115. {
  116.     FDATE a;
  117.     unsigned int b;
  118. }
  119. dc;
  120.  
  121. union
  122. {
  123.     FTIME a;
  124.     unsigned int b;
  125. }
  126. tc;
  127.  
  128. static FILEFINDBUF3 InfoBuf;
  129.  
  130. HDIR hDir;
  131. ULONG cSearch;
  132. ULONG usAttrib;
  133.  
  134. #define FILENAMELEN 255
  135.  
  136. int dir_findfirst(char *filename, int attribute, struct _dta *dta)
  137. {
  138.     hDir = 0x0001;
  139.     usAttrib = attribute;
  140.     cSearch = 1;
  141.  
  142.     if (DosFindFirst(filename, &hDir, usAttrib, &InfoBuf, sizeof InfoBuf,
  143.                      &cSearch, FIL_STANDARD) != 0)
  144.     {
  145.         DosFindClose(hDir);
  146.         errno = DIRUTE_NONE;
  147.         return (-1);
  148.     }
  149.     else
  150.     {
  151.         dta->attrib = (char)InfoBuf.attrFile;
  152.         dta->size = InfoBuf.cbFile;
  153.         dc.a = InfoBuf.fdateLastWrite;
  154.         tc.a = InfoBuf.ftimeLastWrite;
  155.         dta->wr_time = tc.b;
  156.         dta->wr_date = dc.b;
  157.         strcpy(dta->name, InfoBuf.achName);
  158.         errno = 0;
  159.         return (0);
  160.     }
  161. }
  162.  
  163. int dir_findnext(struct _dta *dta)
  164. {
  165.  
  166.     if ((DosFindNext(hDir, &InfoBuf, (USHORT) (FILENAMELEN + 23), &cSearch)) || (cSearch != 1))
  167.     {
  168.         DosFindClose(hDir);
  169.         errno = DIRUTE_NONE;
  170.         return (-1);
  171.     }
  172.     else
  173.     {
  174.         dta->attrib = (char)InfoBuf.attrFile;
  175.         dta->size = InfoBuf.cbFile;
  176.         dc.a = InfoBuf.fdateLastWrite;
  177.         tc.a = InfoBuf.ftimeLastWrite;
  178.         dta->wr_time = tc.b;
  179.         dta->wr_date = dc.b;
  180.         strcpy(dta->name, InfoBuf.achName);
  181.         errno = 0;
  182.         return (0);
  183.     }
  184. }
  185.  
  186. #elif defined(__TURBOC__)
  187.  
  188. #include <dos.h>
  189. #include <dir.h>
  190. #include <string.h>
  191.  
  192. static struct ffblk ffblk;
  193.  
  194. int dir_findfirst(char *filename, int attribute, struct _dta *dta)
  195. {
  196.     int done;
  197.  
  198.     done = findfirst(filename, &ffblk, attribute);
  199.  
  200.     if (!done)
  201.     {
  202.         strcpy(dta->name, ffblk.ff_name);
  203.         dta->wr_time = ffblk.ff_fdate;
  204.         dta->wr_date = ffblk.ff_ftime;
  205.         dta->size = ffblk.ff_fsize;
  206.         dta->attrib = ffblk.ff_attrib;
  207.         return 0;
  208.     }
  209.     return -1;
  210. }
  211.  
  212. int dir_findnext(struct _dta *dta)
  213. {
  214.     if (!findnext(&ffblk))
  215.     {
  216.         strcpy(dta->name, ffblk.ff_name);
  217.         dta->wr_time = ffblk.ff_fdate;
  218.         dta->wr_date = ffblk.ff_ftime;
  219.         dta->size = ffblk.ff_fsize;
  220.         dta->attrib = ffblk.ff_attrib;
  221.         return 0;
  222.     }
  223.     return -1;
  224. }
  225.  
  226. #elif defined(PACIFIC)
  227.  
  228. #include "rfind1st.h"
  229.  
  230. static struct DSTRUCT dstr;
  231.  
  232. int dir_findfirst(char *filename, int attribute, struct _dta *dta)
  233. {
  234.     if (rfind_1st(filename, attribute, &dstr) != NULL)
  235.     {
  236.         strcpy(dta->name, dstr.NAME);
  237.         dta->wr_time = dstr.DATE;
  238.         dta->wr_date = dstr.TIME;
  239.         dta->size = dstr.FSIZE;
  240.         dta->attrib = dstr.ATTRIBUTE;
  241.         return 0;
  242.     }
  243.     return -1;
  244. }
  245.  
  246. int dir_findnext(struct _dta *dta)
  247. {
  248.     if (rfind_nxt(&dstr) != NULL)
  249.     {
  250.         strcpy(dta->name, dstr.NAME);
  251.         dta->wr_time = dstr.DATE;
  252.         dta->wr_date = dstr.TIME;
  253.         dta->size = dstr.FSIZE;
  254.         dta->attrib = dstr.ATTRIBUTE;
  255.         return 0;
  256.     }
  257.     return -1;
  258. }
  259.  
  260. #elif defined(SASC)
  261. #include <dos.h>
  262.  
  263. extern int __msflag;
  264. static struct FileInfoBlock info;
  265. static int error;
  266. static int oldmsf;
  267.  
  268. int dir_findfirst(char *filename, int attribute, struct _dta *dta)
  269. {
  270.     oldmsf = __msflag;
  271.     __msflag = 1;
  272.     error = dfind(&info, filename, 0);
  273.     __msflag = oldmsf;
  274.     while (error == 0)
  275.     {
  276.         strcpy(dta->name, info.fib_FileName);
  277.         return (0);
  278.     }
  279.     return (-1);
  280. }
  281.  
  282. int dir_findnext(struct _dta *dta)
  283. {
  284.     oldmsf = __msflag;
  285.     __msflag = 1;
  286.     error = dnext(&info);
  287.     __msflag = oldmsf;
  288.     if (error == 0)
  289.     {
  290.         strcpy(dta->name, info.fib_FileName);
  291.         return (0);
  292.     }
  293.     return (-1);
  294. }
  295.  
  296. #elif defined(UNIX)
  297.  
  298. /*
  299.    **  support for unix goes here - use the svr3 (posix?) routines:
  300.    **    opendir, scandir, readdir, etc.
  301.  */
  302.  
  303. #include <unistd.h>
  304. #include <sys/types.h>
  305. #include <dirent.h>
  306. #include <stdio.h>
  307. #include "patmat.h"
  308.  
  309. static DIR *dir;
  310. static struct dirent *de;
  311. static char firstbit[FILENAME_MAX];
  312. static char lastbit[FILENAME_MAX];
  313.  
  314. int dir_findfirst(char *filename, int attribute, struct _dta *dta)
  315. {
  316.     char *p;
  317.     int fin = 0;
  318.  
  319.     p = strrchr(filename, '/');
  320.     if (p == NULL)
  321.     {
  322.         strcpy(firstbit, ".");
  323.         strcpy(lastbit, filename);
  324.     }
  325.     else
  326.     {
  327.         memcpy(firstbit, filename, p - filename);
  328.         firstbit[p - filename] = '\0';
  329.         strcpy(lastbit, p + 1);
  330.     }
  331.     dir = opendir(firstbit);
  332.     if (dir == NULL)
  333.     {
  334.         return (-1);
  335.     }
  336.     while (!fin)
  337.     {
  338.         de = readdir(dir);
  339.         if (de == NULL)
  340.         {
  341.             closedir(dir);
  342.             return (-1);
  343.         }
  344.         if (patmat(de->d_name, lastbit))
  345.         {
  346.             fin = 1;
  347.         }
  348.     }
  349.     strcpy(dta->name, de->d_name);
  350.     dta->size = 200;
  351.     return (0);
  352. }
  353.  
  354. int dir_findnext(struct _dta *dta)
  355. {
  356.     int fin = 0;
  357.  
  358.     while (!fin)
  359.     {
  360.         de = readdir(dir);
  361.         if (de == NULL)
  362.         {
  363.             closedir(dir);
  364.             return (-1);
  365.         }
  366.         if (patmat(de->d_name, lastbit))
  367.         {
  368.             fin = 1;
  369.         }
  370.     }
  371.     strcpy(dta->name, de->d_name);
  372.     dta->size = 200;
  373.     return (0);
  374. }
  375.  
  376. #else                           /* MSC version of these routines */
  377.  
  378. #include <dos.h>
  379.  
  380. struct find_t ffblk;
  381.  
  382. int dir_findfirst(char *filename, int attribute, struct _dta *dta)
  383. {
  384.     int done;
  385.  
  386.     done = _dos_findfirst(filename, attribute, &ffblk);
  387.  
  388.     if (!done)
  389.     {
  390.         strcpy(dta->name, ffblk.name);
  391.         dta->wr_time = ffblk.wr_date;
  392.         dta->wr_date = ffblk.wr_time;
  393.         dta->size = ffblk.size;
  394.         dta->attrib = ffblk.attrib;
  395.         return 0;
  396.     }
  397.     return -1;
  398. }
  399.  
  400. int dir_findnext(struct _dta *dta)
  401. {
  402.     if (!_do